#ifndef QNBCONFIG_H
#define QNBCONFIG_H

#include <QObject>
#include <QSettings>

#include "QNBConstant.h"

class QNBConfig:public QObject
{
    Q_OBJECT

public:
    QNBConfig();
    ~QNBConfig();

    void init();
    void saveConfg();

//    Q_PROPERTY(int miHeartInterval READ heartInterval WRITE setHeartInterval USER true)
//    Q_PROPERTY(int miUpdateConfigInterval READ updateConfigInterval WRITE setUpdateConfigInterval USER true)
//    Q_PROPERTY(int miDeleteUserDataDelayTime READ deleteUserDataDelayTime WRITE setDeleteUserDataDelayTime USER true)
//    Q_PROPERTY(int miSubmitPhotoInterval READ submitPhotoInterval WRITE setSubmitPhotoInterval USER true)
//    Q_PROPERTY(int miLockscreenJudgeMaxCount READ lockscreenJudgeMaxCount WRITE setLockscreenJudgeMaxCount USER true)

    int heartInterval() {
        return miHeartInterval;
    }

    int updateConfigInterval()
    {
        return miUpdateConfigInterval;
    }

    int deleteUserDataDelayTime()
    {
        return miDeleteUserDataDelayTime;
    }

    int submitPhotoInterval()
    {
        return miSubmitPhotoInterval;
    }

    int lockscreenJudgeMaxCount()
    {
        return miLockscreenJudgeMaxCount;
    }

    bool setHeartInterval(int iInterval) {
        bool bRet = false;
        if (iInterval != this->miHeartInterval
                && iInterval > 0 && iInterval < HEART_INTERVAL*5) {
            miHeartInterval = iInterval;
            bRet = true;
        }

        return bRet;
    }

    bool setUpdateConfigInterval(int iInterval) {
        if (iInterval != this->miUpdateConfigInterval
                && iInterval > 0 && iInterval < UPDATE_CONFIG_INTERVAL*5) {
            miUpdateConfigInterval = iInterval;

            return true;
        }

        return false;
    }

    bool setDeleteUserDataDelayTime(int iInterval) {
        if (iInterval != this->miDeleteUserDataDelayTime
                && iInterval > 0 && iInterval < DELETE_USER_DATA_DELAY_TIME*5) {
            miDeleteUserDataDelayTime = iInterval;
            return true;
        }

        return false;
    }

    bool setLockscreenJudgeMaxCount(int count)
    {
        if (count != this->miLockscreenJudgeMaxCount
                && count > 0) {
            miLockscreenJudgeMaxCount = count;

            return true;
        }

        return false;
    }

    bool setSubmitPhotoInterval(int iInterval)
    {
        if (iInterval != this->miSubmitPhotoInterval
                && iInterval > 0 && iInterval < SUBMIT_PHOTO_INTERVAL*2) {
            miSubmitPhotoInterval = iInterval;
            return true;
        }

        return false;
    }

    bool setCheckPhotoSimilar(int iSimilar)
    {
        if (iSimilar != this->miCheckPhotoSimilar
                && iSimilar >= 50 && iSimilar <= 100) {
            miCheckPhotoSimilar = iSimilar;

            return true;
        }

        return false;
    }

    int checkPhotoSimilar()
    {
        return this->miCheckPhotoSimilar;
    }

    bool setCheckPhotoInterval(int iInterval)
    {
        if (iInterval != this->miCheckPhotoInterval
                && iInterval >= 10 && iInterval <= CHECK_PHOTO_INTERVAL*5) {
            miCheckPhotoInterval = iInterval;

            return true;
        }

        return false;
    }

    int checkPhotoInterval()
    {
        return this->miCheckPhotoInterval;
    }


    bool setLoopCheckLoginStatusInterval(int iInterval)
    {
        if (iInterval != this->miLoopCheckLoginStatusInterval
                && iInterval >= LOOP_CHECK_LOGIN_STATUS_INTERVAL/2 && iInterval <= LOOP_CHECK_LOGIN_STATUS_INTERVAL*5) {
            miLoopCheckLoginStatusInterval = iInterval;

            return true;
        }

        return false;
    }

    int loopCheckLoginStatusInterval()
    {
        return this->miLoopCheckLoginStatusInterval;
    }

    QString getUserDataDir()
    {
         return mstrUserDataDir;
    }

    bool setServerIP(QString strServerIP)
    {
        if (strServerIP != mstrServerIP) {
            mstrServerIP = strServerIP;

            return true;
        }

        return false;
    }

    QString getServerIP()
    {
         return mstrServerIP;
    }

    QString getCheckPhotoServerUrl()
    {
         return mstrCheckPhotoServerIP;
    }

    bool setCameraDeviceIndex(int iCameraDeviceIndex)
    {
        if (miCameraDeviceIndex != iCameraDeviceIndex) {
            miCameraDeviceIndex = iCameraDeviceIndex;

            return true;
        }

        return false;
    }

    int getCameraDeviceIndex() {
        return miCameraDeviceIndex;
    }

    QString getCameraDevice() {
        return getCameraDevices(miCameraDeviceIndex);
    }

    bool setCheckPhotoUseBase64(int base64)
    {
        if (miCheckPhotoUseBase64 == 0 && base64 != 0) {
            miCheckPhotoUseBase64 = base64;

            return true;
        } else if (miCheckPhotoUseBase64 != 0 && base64 == 0) {
            miCheckPhotoUseBase64 = base64;

            return true;
        }

        return false;
    }

    int checkPhotoUseBase64()
    {
        return this->miCheckPhotoUseBase64;
    }



    static QNBConfig* getInstance();


    QString getServerHttpController();

    QString getConfigFilePath();

private:
    void loadConfg();
    void setNodeInt(QSettings* cfg, const QString strName, int value);
    void getNodeInt(QSettings *cfg, QString strName, int defaultValue, int &value, int min, int max);

    void setNodeString(QSettings *cfg, QString strFieldName, QString &strValue);
    void getNodeString(QSettings *cfg, QString strFieldName, QString defaultValue, QString &strValue);


    QString getCameraDevices(int index);
    bool QNBConfig::listCameraDevices();
private:
    int miHeartInterval;
    int miUpdateConfigInterval;
    int miDeleteUserDataDelayTime;
    int miSubmitPhotoInterval;
    int miLockscreenJudgeMaxCount;//没保存到配置文件里。
    int miCheckPhotoSimilar;
    int miCheckPhotoInterval;//实时头像比对间隔时间
    int miLoopCheckLoginStatusInterval;//轮询监测登录、登出状态的时间间隔。

    int miCheckPhotoUseBase64;//非0，提交base64内容比对； 0，提交物理路径比对; 默认是0，物理路径比对。
    int miCameraDeviceIndex;
    QString mstrServerIP;
    QString mstrCheckPhotoServerIP;
    QString mstrConfigFile;
    QString mstrUserDataDir;

    QStringList mCameraDevicesList;

private:
    static QNBConfig *gCfg;
};


#endif // QNBCONFIG_H
